//
//  MyGradesContentCell.m
//  WanCaiC8
//
//  Created by tangang on 2018/4/27.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "MyGradesContentCell.h"

@implementation MyGradesContentCell{
    QXLable     *labName;
    QXLable     *labBet;
    UIButton    *btnResults;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle=UITableViewCellSeparatorStyleNone;
        labName=[[QXLable alloc]init];
        labName.textAlignment=NSTextAlignmentCenter;
        [self.contentView addSubview:labName];
        [labName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView);
            make.centerY.equalTo(self.contentView);
            make.width.mas_equalTo(@100);
        }];
        
        UIView *line=[[UIView alloc]init];
        line.backgroundColor=DEFAULTLINECOLOR;
        [self.contentView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView);
            make.left.equalTo(self.contentView).with.offset(100);
            make.width.mas_equalTo(@.7);
            make.bottom.equalTo(self.contentView);
        }];
        
        labBet=[[QXLable alloc]init];
        [self.contentView addSubview:labBet];
        [labBet mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(line).with.offset(15);
            make.right.equalTo(self.contentView).with.offset(-100);
            make.top.bottom.equalTo(self.contentView);
            make.height.mas_equalTo(@35);
        }];
        
        UIView *line1=[[UIView alloc]init];
        line1.backgroundColor=DEFAULTLINECOLOR;
        [self.contentView addSubview:line1];
        [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView);
            make.right.equalTo(self.contentView).with.offset(-90);
            make.width.mas_equalTo(@.7);
            make.bottom.equalTo(self.contentView);
        }];
        
        btnResults =[[UIButton alloc]init];
        [btnResults.titleLabel setFont:[UIFont systemFontOfSize:13]];
        [self.contentView addSubview:btnResults];
        [btnResults mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.right.bottom.equalTo(self.contentView);
            make.width.mas_equalTo(@90);
        }];
        
        UIView *line2=[[UIView alloc]init];
        line2.backgroundColor=DEFAULTLINECOLOR;
        [self.contentView addSubview:line2];
        [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.contentView);
            make.height.mas_equalTo(@.7);
        }];
    }
    return self;
}

- (void)setArr:(NSArray *)arr{
    _arr=arr;
}

- (void)setModel:(BetModel *)model{
    _model=model;
    LotteryNumType lType=[[BaseVM manage] getType:model.lType];
    if (lType==LotteryNumTypeLottery) {
        [labBet mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@60);
        }];
    }
    
    labName.text=model.PlayName;
    labBet.attributedText=[self attStr];
    if (model.Score>0) {
        [btnResults setTitleColor:NAVCOLOR forState:UIControlStateNormal];
        [btnResults setImage:[UIImage imageNamed:@"bet_right"] forState:UIControlStateNormal];
        [btnResults setTitle:[NSString stringWithFormat:@" +%.0f",model.Score] forState:UIControlStateNormal];
    }else{
        [btnResults setTitleColor:DEFAULTTEXTBLACKCOLOR forState:UIControlStateNormal];
        [btnResults setImage:[UIImage imageNamed:@"bet_error"] forState:UIControlStateNormal];
        [btnResults setTitle:[NSString stringWithFormat:@" %.0f",model.Score] forState:UIControlStateNormal];
    }
}

- (NSMutableAttributedString *)attStr{
    NSMutableAttributedString *attStr=[[NSMutableAttributedString alloc]initWithString:self.model.BetNum];
    NSArray *arrNum=[self.model.BetNum componentsSeparatedByString:@","];
    LotteryNumType type=[[BaseVM manage] getType:self.model.lType];
    if (type==LotteryNumTypeCarPK10||type==LotteryNumTypeLuckyAirship){
        NSInteger index=0;
        if ([_model.PlayName isEqualToString:@"冠军五码"]) {
            index=0;
        }else if ([_model.PlayName isEqualToString:@"亚军五码"]){
            index=1;
        }else if ([_model.PlayName isEqualToString:@"第三名五码"]){
            index=2;
        }else if ([_model.PlayName isEqualToString:@"第四名五码"]){
            index=3;
        }else if ([_model.PlayName isEqualToString:@"第五名五码"]){
            index=4;
        }else if ([_model.PlayName isEqualToString:@"第六名五码"]){
            index=5;
        }else if ([_model.PlayName isEqualToString:@"第七名五码"]){
            index=6;
        }else if ([_model.PlayName isEqualToString:@"第八名五码"]){
            index=7;
        }else if ([_model.PlayName isEqualToString:@"第九名五码"]){
            index=8;
        }else if ([_model.PlayName isEqualToString:@"第十名五码"]){
            index=9;
        }
        if (self.arr.count>index) {
            NSString *str=self.arr[index];
            for (NSString *strNum in arrNum) {
                if ([str isEqualToString:strNum]) {
                    NSRange rang=[self.model.BetNum rangeOfString:strNum];
                    [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                    break;
                }
            }
        }
        
    }else if (type==LotteryNumTypeBrings) {
        if ([_model.PlayName isEqualToString:@"前区4码"]) {
            for (NSString *str in arrNum) {
                for (int i=0;i<self.arr.count-2;i++) {
                    NSString *strNum=self.arr[i];
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[self.model.BetNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }else if ([_model.PlayName isEqualToString:@"后区3码"]){
            if (self.arr.count!=7) {
                return attStr;
            }
            for (NSString *str in arrNum) {
                for (int i=5;i<self.arr.count;i++) {
                    NSString *strNum=self.arr[i];
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[self.model.BetNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }
    }else if (type==LotteryNumTypeLottery3D||type==LotteryNumType11Select5||type==LotteryNumTypeArrangement3||type==LotteryNumTypeArrangement5||type==LotteryNumTypeSevenLecai||type==LotteryNumTypeSevenStars||type==LotteryNumTypeAllTime) {
        if ([_model.PlayName isEqualToString:@"第一球五码"]) {
            if (self.arr.count>0) {
                NSString *str=self.arr[0];
                for (NSString *strNum in arrNum) {
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[self.model.BetNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }else if ([_model.PlayName isEqualToString:@"第二球五码"]){
            if (self.arr.count>1) {
                NSString *str=self.arr[1];
                for (NSString *strNum in arrNum) {
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[self.model.BetNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }else if ([_model.PlayName isEqualToString:@"第三球五码"]){
            if (self.arr.count>2) {
                NSString *str=self.arr[2];
                for (NSString *strNum in arrNum) {
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[self.model.BetNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }else if ([_model.PlayName isEqualToString:@"第四球五码"]){
            if (self.arr.count>3) {
                NSString *str=self.arr[3];
                for (NSString *strNum in arrNum) {
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[self.model.BetNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }else if ([_model.PlayName isEqualToString:@"第五球五码"]){
            if (self.arr.count>4) {
                NSString *str=self.arr[4];
                for (NSString *strNum in arrNum) {
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[self.model.BetNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }else if ([_model.PlayName isEqualToString:@"第六球五码"]){
            if (self.arr.count>5) {
                NSString *str=self.arr[5];
                for (NSString *strNum in arrNum) {
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[self.model.BetNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }else if ([_model.PlayName isEqualToString:@"第七球五码"]){
            if (self.arr.count>6) {
                NSString *str=self.arr[6];
                for (NSString *strNum in arrNum) {
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[self.model.BetNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }else if ([_model.PlayName isEqualToString:@"胆一码"]||[_model.PlayName isEqualToString:@"胆二码"]||[_model.PlayName isEqualToString:@"胆三码"]){
            for (NSString *str in arrNum) {
                for (NSString *strNum in self.arr) {
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[self.model.BetNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }
    }else if (type==LotteryNumTypeHappy10){
        return attStr;
    }else if (type==LotteryNumTypeNearlyThree){
        if ([_model.PlayName isEqualToString:@"独胆"]||[_model.PlayName isEqualToString:@"双胆"]) {
            for (NSString *str in arrNum) {
                for (NSString *strNum in self.arr) {
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[self.model.BetNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }
    }else if (type==LotteryNumTypeLottery){
        if ([_model.PlayName isEqualToString:@"十码中特"]||[_model.PlayName isEqualToString:@"二十码中特"]) {
            for (NSString *str in arrNum) {
                NSString *strNum=self.arr.lastObject;
                if ([str isEqualToString:strNum]) {
                    NSRange rang=[self.model.BetNum rangeOfString:strNum];
                    [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                    break;
                }
            }
        }else if ([_model.PlayName isEqualToString:@"平五码"]){
            for (NSString *str in arrNum) {
                for (int i=0;i<self.arr.count-1;i++) {
                    NSString *strNum=self.arr[i];
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[self.model.BetNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }
    }else if (type==LotteryNumTypeChromosphere){
        if ([_model.PlayName isEqualToString:@"红3胆"]||[_model.PlayName isEqualToString:@"红2胆"]) {
            for (NSString *str in arrNum) {
                for (int i=0;i<self.arr.count-1;i++) {
                    NSString *strNum=self.arr[i];
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[self.model.BetNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }else if ([_model.PlayName isEqualToString:@"蓝8码"]||[_model.PlayName isEqualToString:@"蓝5码"]){
            for (NSString *str in arrNum) {
                NSString *strNum=self.arr.lastObject;
                if ([str isEqualToString:strNum]) {
                    NSRange rang=[self.model.BetNum rangeOfString:strNum];
                    [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                    break;
                }
            }
        }
    }else if (type==LotteryNumTypeHappy12){
        NSInteger index;
        if ([_model.PlayName isEqualToString:@"第一球六码"]){
            index=0;
        }else if ([_model.PlayName isEqualToString:@"第二球六码"]){
            index=1;
        }else if ([_model.PlayName isEqualToString:@"第三球六码"]){
            index=2;
        }else if ([_model.PlayName isEqualToString:@"第四球六码"]){
            index=3;
        }else if ([_model.PlayName isEqualToString:@"第五球六码"]){
            index=4;
        }else{
            index=100;
        }
        
        if (self.arr.count>index) {
            NSString *str=self.arr[index];
            for (NSString *strNum in arrNum) {
                if ([str isEqualToString:strNum]) {
                    NSRange rang=[self.model.BetNum rangeOfString:strNum];
                    [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                    break;
                }
            }
        }
    }
    return attStr;
}


@end
